#include "user_include.h"
static void drv_init(void);
static int8_t drv_write(struct tm t);
static int8_t drv_read(struct tm *t);
static void drv_task(void);
struct driver_rtc_struct_def driver_rtc_struct =
    {
        .init = drv_init,
        .write = drv_write,
        .read = drv_read,
        .task = drv_task,
};
static void drv_init(void)
{
}
static int8_t drv_write(struct tm t)
{
    RTC_TimeTypeDef sTime;
    RTC_DateTypeDef sDate;

    sDate.Year = t.tm_year;
    sDate.Month = t.tm_mon;
    sDate.Date = t.tm_mday;
    sDate.WeekDay = t.tm_wday;
    sTime.Hours = t.tm_hour;
    sTime.Minutes = t.tm_min;
    sTime.Seconds = t.tm_sec;
    sTime.TimeFormat = RTC_HOURFORMAT12_PM;
    sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
    sTime.StoreOperation = RTC_STOREOPERATION_RESET;
    HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN);
    HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN);
    return 0;
}
static int8_t drv_read(struct tm *t)
{
    RTC_DateTypeDef sDate;
    RTC_TimeTypeDef sTime;
    HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN);
    HAL_RTC_GetDate(&hrtc, &sDate, RTC_FORMAT_BIN);
    t->tm_year = sDate.Year;
    t->tm_mon = sDate.Month;
    t->tm_mday = sDate.Date;
    t->tm_wday = sDate.WeekDay;
    t->tm_hour = sTime.Hours;
    t->tm_min = sTime.Minutes;
    t->tm_sec = sTime.Seconds;
    return 0;
}
static void drv_task(void)
{
    struct tm t;
    uint32_t i = 0;
    drv_read(&t);
    while (1)
    {
        t.tm_sec++;
        if (t.tm_sec >= 30)
        {
            t.tm_sec = 0;
        }
        drv_write(t);
        drv_read(&t);
        osDelay(1000);
    }
}